<!--
 * @Description: 开单车辆选择
 * @Autor: 何兴家
 * @Date: 2020-12-07 10:39:08
 * @LastEditors: 马毅
 * @LastEditTime: 2021-09-28 17:55:50
-->
<template>
  <div>
    <TopFilter ref="topFilter" :cols="4" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="repair_repairReception_customer_vehicleChoice"
      height="auto"
      :maxHeight="300"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      selectionType="single"
      :onSyncTableData="handleSyncTableData"
      :onRowSelectChange="handleTableRowSelect"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <FormPanel ref="formPanel" :cols="2" :list="formList" :isSubmitBtn="false" :labelWidth="100"></FormPanel>

    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
      <AjaxButton type="primary" :click="handleConfirm">确定</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, jsonToHump } from '@/utils';
import { getAppId } from '@/assets/js/auth';

import { queryRepairVehicleList } from '@/api/repair/common';
import { getUserByVinAndMobileFromJH } from '@/api/repair/repairReception/customer';

export default {
  mixins: [dictionary],
  props: {
    vinOne: {
      type: String,
      default: ''
    }
  },
  data() {
    this.selectedRows = []; // 选中的行项
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      formList: this.createFormList(),
      fetchapi: queryRepairVehicleList,
      fetchParams: { xhrAbort: true },
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    console.log(this.vinOne, 'vinOne');
    if (this.vinOne && this.vinOne != null && this.vinOne != '') {
      this.topFilterList.find(x => x.fieldName == 'vin').initialValue = this.vinOne;
      this.handleSearch({ vin: this.vinOne });
    }
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'ownerName'
        }
      ];
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '上次进厂里程',
          fieldName: 'LAST_MAINTAIN_MILEAGE',
          initialValue: '',
          readonly: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '本次进厂里程',
          fieldName: 'IN_MILEAGE',
          isControls: false,
          precision: 2,
          rules: [{ required: true, message: '请输入进厂行驶里程', trigger: 'blur' }]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          width: 180,
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          width: 180,
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '品牌',
          dataIndex: 'BRAND_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '车系',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '上次进厂里程',
          dataIndex: 'LAST_MAINTAIN_MILEAGE',
          sorter: true
        }
      ];
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 点击搜索
    handleSearch(e) {
      this.fetchParams = Object.assign({ xhrAbort: false }, e);
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    // 行项选中函数
    handleTableRowSelect(list) {
      if (list.length) {
        this.formList.find(item => item.fieldName === 'LAST_MAINTAIN_MILEAGE').initialValue = list[0].LAST_MAINTAIN_MILEAGE;
        this.formList.find(item => item.fieldName === 'IN_MILEAGE').min = list[0].LAST_MAINTAIN_MILEAGE;
        this.formList.find(item => item.fieldName === 'IN_MILEAGE').initialValue = '';
        this.selectedRows = list;
      }
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    },
    // 点击确定
    async handleConfirm() {
      if (!this.selectedRows.length) return notifyAction('请选择车辆', 'warning');
      let formData = await this.$refs.formPanel.GET_FORM_DATA();
      if (formData[0]) return;
      if (formData[1].IN_MILEAGE == 0) return notifyAction('本次进厂里程不能为0', 'warning');
      if (formData[1].LAST_MAINTAIN_MILEAGE > formData[1].IN_MILEAGE) return notifyAction('本次进厂里程不能小于上次出厂里程', 'warning');
      // if (getAppId() != 'beijing') {
      //   let res = await getUserByVinAndMobileFromJH({ vin: this.selectedRows[0].VIN, mobile: this.selectedRows[0].CONTACTOR_MOBILE });
      //   if (res.resultCode == 200 && res.data != 1) {
      //     //  1的时候是符合条件0不符合
      //     return notifyAction('此车辆未在APP注册，请先前往APP注册', 'warning');
      //   }
      // }

      let emitData = { ...this.selectedRows[0], ...formData[1] };
      jsonToHump(emitData);

      // 去除送修人自动带出逻辑，改为手动选择 hxj 20210318
      delete emitData.deliverer;
      delete emitData.delivererMobile;

      this.$emit('onConfirm', emitData);
    }
  }
};
</script>
